using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.CodeRush.StructuralParser;

namespace DX_SourceOutliner
{
    public class IconNodeProcessor : INodeProcessor
    {
        private ISourceTreeNode _source;
        private ICollection<MemberVisibility> _visibilitiesWithCustomIcons = new List<MemberVisibility>();

        /// <summary>
        /// Initializes a new instance of the IconProcessor class.
        /// </summary>
        public IconNodeProcessor()
        {

            _visibilitiesWithCustomIcons.Add(MemberVisibility.Public);
            _visibilitiesWithCustomIcons.Add(MemberVisibility.Protected);
            _visibilitiesWithCustomIcons.Add(MemberVisibility.Private);
        }

        public TreeNode Process(TreeNode node)
        {
            _source = (ISourceTreeNode)node.Tag;

            if (_source.IsDocument)
                SetIconFormDocumentElement(node);
            else
                SetIconForCodeElement(node);

            return node;
        }

        private void SetIconForCodeElement(TreeNode node)
        {
            var memberElement = _source.Element as IMemberElement;
            var codeElement = _source.Element as CodeElement;

            if (memberElement == null && codeElement == null) return;

            string imageKey = string.Empty;

            //default setting for icon is the combination of type + visibility
            if (memberElement != null)
            {
                if (_visibilitiesWithCustomIcons.Contains(memberElement.Visibility))
                    imageKey = string.Format("{0}_{1}", memberElement.ElementType.ToString(), _source.MemberVisibilityText);
                else
                    imageKey = string.Format("{0}_Public", memberElement.ElementType.ToString());
            }

            //if its a namespace, its not an IMemeberElement so no visibility settings...
            if (codeElement != null && codeElement.ElementType == LanguageElementType.Namespace)
                imageKey = "Scope_Namespace";

            node.ImageKey = imageKey;
            
            node.SelectedImageKey = node.ImageKey;

        }

        public void SetIconFormDocumentElement(TreeNode node)
        {
            switch (System.IO.Path.GetExtension(_source.ElementName).ToLower())
            {
                case ".cs":
                    node.ImageKey = "Document_CS";
                    break;
                case ".vb":
                    node.ImageKey = "Document_VB";
                    break;
                case ".js":
                    node.ImageKey = "Document_JS";
                    break;
                default:
                    node.ImageKey = "Document";
                    break;
            }

            node.SelectedImageKey = node.ImageKey;

        }

    }
}
